package net.xinqushi.action;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import net.xinqushi.model.Album;
import net.xinqushi.model.User;
import net.xinqushi.service.AlbumService;
import net.xinqushi.service.UserService;

public class AlbumAction{
	private Album album;
	private File myfile;
	private String myfileFileName;
	private String myfileContentType;
	private UserService userService;
	private AlbumService albumService;
	
	public String upload() throws IOException
	{
		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
		System.out.println("realpath: "+realpath);
		if(myfile!=null)
		{
			File savefile= new File(new File(realpath),myfileFileName);
			if(!savefile.getParentFile().exists())
			{
				savefile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(myfile, savefile);	
		}
		HttpServletRequest request =ServletActionContext.getRequest();
		album.setCover(request.getContextPath()+"/images/" + myfileFileName);
		User user=(User) ServletActionContext.getContext().getSession().get("USER");
		user=userService.get(user);
		album.setUser(user);
		albumService.add(album);
		return "album";
	}
	
	public Album getAlbum() {
		return album;
	}
	public void setAlbum(Album album) {
		this.album = album;
	}
	
	public UserService getService() {
		return userService;
	}
	@Resource(name="userservice")
	public void setService(UserService userService) {
		this.userService = userService;
	}
	public AlbumService getAlbumService() {
		return albumService;
	}
	@Resource(name="albumservice")
	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}

	public File getMyfile() {
		return myfile;
	}

	public void setMyfile(File myfile) {
		this.myfile = myfile;
	}

	public String getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(String myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

	public String getMyfileContentType() {
		return myfileContentType;
	}

	public void setMyfileContentType(String myfileContentType) {
		this.myfileContentType = myfileContentType;
	}
	
}
